package com.example.zhangxinying.smratchatnew;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxinying on 16/9/7.
 */
public class SmartChatFragment extends Fragment implements View.OnClickListener {
    private ListView mlvData;
    private RecyclerView mlvTag;
    private LinearLayoutManager mLayoutManager;
    private LinearLayout mLlsmart;
    private InputMethodManager manager = null;
    public static EditText et_row;
    private int spacingInPixels;
    private LinearLayout mLlTag;
    private TagBean tagbean;
    private TelephoneBean telephoneBean;
    private LinearLayout mLltel;
    private LinearLayout tv_send;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragement_smart, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        init(view);
        mlvData.setAdapter(getDdataAdapter());
        spacingInPixels=getResources().getDimensionPixelSize(R.dimen.space);
        mlvTag.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mlvTag.setLayoutManager(mLayoutManager);
        mlvTag.setAdapter(getTagAdapter());
        return view;

    }

    private void init(View v){
        tagbean  = new TagBean();
        tagbean.setVisible(true);
        telephoneBean=new TelephoneBean();
        telephoneBean.setVisible(false);
//        tagbean.setData(getMtag());
        et_row = (EditText) v.findViewById(R.id.et_row);
        mlvData = (ListView) v.findViewById(R.id.lv_data);
        mlvTag = (RecyclerView) v.findViewById(R.id.lv_tag);
        mLlTag=(LinearLayout)v.findViewById(R.id.ll_tag);
        mLltel=(LinearLayout)v.findViewById(R.id.ll_telephone);
        tv_send = (LinearLayout)v.findViewById(R.id.tv_send);
        tv_send.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"111",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mLlsmart = (LinearLayout) getActivity().findViewById(R.id.lv_smart);
//        mLlsmart.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//                if (imm.isActive()) {
//                    imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
//                    et_row.clearFocus();
//                }
//
//            }
//        });
        et_row.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mlvData.setSelection(getMyData().size()-1);
                }
            }
        });

        setTag();
        setTelephone();
    }



    private void setTag(){
        if(tagbean.isVisible()){
            mLlTag.setVisibility(View.VISIBLE);
        }else{
            mLlTag.setVisibility(View.GONE);
        }

    }

    private void setTelephone(){
        if (telephoneBean.isVisible()){
            mLltel.setVisibility(View.VISIBLE);
        }else {
            mLltel.setVisibility(View.GONE);
        }

    }


    private BaseAdapter getDdataAdapter() {

        return new ChatAdapter(getActivity(), getMyData());
    }

    private TagAdapter getTagAdapter() {
        return new TagAdapter(getActivity(),getMtag());
    }




    private List<Message> getMyData() {

        List<Message> msgList = new ArrayList<Message>();
        Message msg;


        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_FIRST_LOGIN);
        msg.setValue("小主人，点小评有什么可以帮您");
        msg.setLeftTag("选餐厅");
        msg.setRightTag("点外卖");
        msgList.add(msg);


//        msgList.add(msg);
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT);
//        msg.setValue("小主人，点小评有什么可以帮您？");
//        msgList.add(msg);
//
        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_TUANGOU_RECYCLEVIEW);
        msgList.add(msg);


        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_RECOMMENDFOOD_RECYCLEVIEW);
        msgList.add(msg);






        msg = new Message();
        msg.setType(ChatAdapter.VALUE_RIGHT_TEXT);
        msg.setValue("给我推荐一个餐厅");
        msgList.add(msg);

//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_SHOP_RECYCLEVIEW);
//        msg.setValue("看看附近有哪些");
//        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_TUANGOU_RECYCLEVIEW);
        msg.setValue("看看附近有哪些");
        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_ORDER_DETAILS);
        msg.setValue("看看附近有哪些");
        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_DEQUEUE_DETAILS);
        msg.setValue("看看附近有哪些");
        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_GET_DEQUEUEID);
        msg.setValue("看看附近有哪些");
        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_TWO_ROWS_TEXT);
        msg.setValue("看看附近有哪些");
        msgList.add(msg);

        msg = new Message();
        msg.setType(ChatAdapter.VALUE_LEFT_TEXT_SPANER);
        msg.setValue("本帮浙江菜");
        msg.setSpanerText("浙江菜");
        msgList.add(msg);
        msg.setType(ChatAdapter.VALUE_LEFT_TRAVEL_SHOP_RANK_RECYCLEVIEW);
        msgList.add(msg);

//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT);
//        msg.setValue("您需要怎样的餐厅");
//        msgList.add(msg);
//
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_RIGHT_TEXT);
//        msg.setValue("看看附近有哪些");
//        msgList.add(msg);
//
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT);
//        msg.setValue("要不要挑个口味");
//        msgList.add(msg);
//
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_RIGHT_TEXT);
//        msg.setValue("本帮浙江菜");
//        msgList.add(msg);
//
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT);
//        msg.setValue("要不要选个价位");
//        msgList.add(msg);
//
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_RIGHT_TEXT);
//        msg.setValue("100-300之间");
//        msgList.add(msg);



//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT_SPANER);
//        msg.setValue("本帮浙江菜");
//        msg.setSpanerText("浙江菜");
//        msgList.add(msg);
//        msg = new Message();
//        msg.setType(ChatAdapter.VALUE_LEFT_TEXT);
//        msg.setValue("点小评已为您推荐餐厅");
//        msgList.add(msg);

        return msgList;

    }


    private List<Message> getMtag() {
        List<Message> msglist = new ArrayList<Message>();
        Message msg;
        msg = new Message();
        msg.setValue("挑餐厅");
        msglist.add(msg);


        msg = new Message();
        msg.setValue("点外卖");
        msglist.add(msg);


        msg = new Message();
        msg.setValue("吃大餐");
        msglist.add(msg);


        msg = new Message();
        msg.setValue("霸王餐");
        msglist.add(msg);

        msg = new Message();
        msg.setValue("烛光晚宴");
        msglist.add(msg);

        return msglist;
    }




    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.lv_smart:
//                InputMethodManager imm = ( InputMethodManager )v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
//                if ( imm.isActive( ) ) {
//                    imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );
//
//
//            }
//
//        }
    }


    public static Boolean hideInputMethod(Context context, View v) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return false;
    }
}
